
package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xebest.api.dto.XeVenFinPurchPayDTO;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.XeVenFinPurchPayForm;
import com.xebest.web.model.XeVenFinPurchPayEntity;
import com.xebest.web.service.XeVenFinPurchPayReadService;
import com.xebest.web.view.ErpUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 付款单同步接口
 *
 * @author 赵盼飞
 */
@RestController
@RequestMapping("restapi/finPurchPay")
public class XeVenFinPurchPayRestApi extends BaseController {

    private static Logger log = LoggerFactory.getLogger(XeVenFinPurchPayRestApi.class);

    /**
     * 财务付款单操作service
     */
    @Autowired
    private XeVenFinPurchPayReadService xeVenFinPurchPayReadService;

    /**
     * 付款单同步接口
     *
     * @author 赵盼飞
     */
    @RequestMapping("/updateFinPurchPayByOrderId")
    public BaseResponseDTO updateFinPurchPayByOrderId(@RequestBody XeVenFinPurchPayDTO finPurchPayDTO) {
        try {
            String paramJson = JSONObject.toJSONString(finPurchPayDTO);
            log.info("付款单同步接口,根据订单号更新付款单信息,传入参数： {}", paramJson);
            //1、验证参数
            if (finPurchPayDTO.getPsettleCodeList().size() > 0 && null != finPurchPayDTO.getPsettleCodeList()) {
                //2、拼装参数
                XeVenFinPurchPayEntity finPurchPayEntity = new XeVenFinPurchPayEntity();
                finPurchPayEntity.setpSettleCodeList(finPurchPayDTO.getPsettleCodeList());
                if (null != finPurchPayDTO.getPsettleStatus()) {
                    finPurchPayEntity.setpSettleStatus(finPurchPayDTO.getPsettleStatus());
                }
                if (null != finPurchPayDTO.getRejectStatus()) {
                    finPurchPayEntity.setRejectStatus(finPurchPayDTO.getRejectStatus());
                }
                if (null != finPurchPayDTO.getSettleDate()) {
                    finPurchPayEntity.setSettleDate(new Date());
                }
                //3、返回结果
                int result = xeVenFinPurchPayReadService.updateFinPurchPayByOrderId(finPurchPayEntity);
                if (result == 0) {
                    return sendApiError(finPurchPayDTO.getSpanId());
                }
            }
        } catch (Exception e) {
            log.error("付款单同步接口,根据订单号更新付款单信息,失败：{}", e);
            return sendApiError(finPurchPayDTO.getSpanId());
        }
        return sendApiOK(finPurchPayDTO.getSpanId());
    }


    /**
     * 修改付款单
     *
     * @author 赵盼飞
     */
    @RequestMapping("updateFinPurchPay")
    public @ResponseBody
    BaseResponse updateBathFinPurchPayByPsettleCodeArr(@RequestBody XeVenFinPurchPayForm pform) {

        log.info("根据付款单号更新结账金额接口,传入参数： {}", JSON.toJSONString(pform));
        try {
            if (null == pform.getpSettleCodeList() || pform.getpSettleCodeList().size() <= 0
                    || StringUtils.isEmpty(pform.getSettleAccountsAmount())) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            //1.获取userinfo
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            pform.setUserCode(userInfo.getUserCode());
            int res = xeVenFinPurchPayReadService.updateBathFinPurchPayByPsettleCodeArr(pform);
            if (res > 0) {
                return sendAjaxOK();
            } else {
                return sendAjaxError();
            }
        } catch (Exception e) {
            log.error("更新结账金额接口异常：{}", e);
            return sendAjaxError();
        }

    }

}
  
